*--------------------------------------------------------*
* Coefplots by sample for multiple outcomes
*--------------------------------------------------------*
  
    cd "${data}"
	use "dataset.dta", clear
    cd "${data}/coefplots"
local outcomes selfplacement redistribution spending regulation immigration environment samesexmarriage laworder euintegration

foreach y of local outcomes {

    *----- Uncontrolled model (y on voted) -----
    cd "${data}"
    use "dataset.dta", clear
    cd "${data}/coefplots"

    drop if missing(`y') | missing(voted)
    keep if samplelimit==1

    set more off
    tempname turnout_file

    postfile `turnout_file' ///
        sample b_voted se_voted p_voted str60 sample_lbl ///
        using "`y'.dta", replace

    levelsof country_year_, local(levels)
    local vlbl : value label country_year_
    local nlevels = r(N)

    foreach s of local levels {
        local t : label `vlbl' `s'
        quietly regress `y' voted if country_year_==`s', robust
        scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))
        post `turnout_file' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
    }
    postclose `turnout_file'

    *----- Controlled model (add covariates) -----
	
    cd "${data}"
    use "dataset.dta", clear
    cd "${data}/coefplots"

    drop if missing(`y') | missing(voted)
    keep if samplelimit==1

    set more off
    tempname turnout_file_c

    postfile `turnout_file_c' ///
        sample b_voted_01 se_voted_01 p_voted_01 str60 sample_lbl ///
        using "`y'_c.dta", replace

    levelsof country_year_, local(levels_c)
    local vlbl_c : value label country_year_

    foreach s of local levels_c {
        local t : label `vlbl_c' `s'
        quietly regress `y' voted i.age i.education i.welloff i.gender ///
            if country_year_==`s', robust
        scalar p_value = 2 * ttail(e(df_r), abs(_b[voted]/_se[voted]))
        post `turnout_file_c' (`s') (_b[voted]) (_se[voted]) (p_value) ("`t'")
    }
    postclose `turnout_file_c'

    *----- Confidence intervals & labeling -----
    cd "${data}/coefplots"
    use "`y'.dta", clear
    gen voted_lower  = b_voted  - 1.96*se_voted
    gen voted_upper  = b_voted  + 1.96*se_voted
    labmask sample, value(sample_lbl)
    save "`y'.dta", replace

    use "`y'_c.dta", clear
    gen voted_lower_01 = b_voted_01 - 1.96*se_voted_01
    gen voted_upper_01 = b_voted_01 + 1.96*se_voted_01
    labmask sample, value(sample_lbl)
    save "`y'_c.dta", replace

    *----- Merge and plot -----
    use "`y'.dta", clear
    merge 1:1 sample using "`y'_c.dta", nogen


tw rspike voted_lower voted_upper sample, lcolor(gs5) || scatter  b_voted  sample, mcolor(gs5) msize(2-pt) msymbol(circle) yline(0) xla(1/57) legend(off) xtitle("") ///
|| rspike voted_lower_01 voted_upper_01 sample, lcolor(lavender%50) lwidth(thick) || scatter  b_voted_01 sample, mcolor(lavender%70) msize(2-pt) msymbol(circle) yline(0) xla(1/57, valuelabels ang(v) labsize(vsmall)) legend(off) xtitle("") yline(0,lcolor(gs5)) ytitle("`y'")

    graph save "${figures}/coefplots_`y'.gph", replace
	graph export "${figures}/coefplots_`y'.png", as(png) replace

}





